package com.ttc.ssm.controller.admin;

import com.ttc.ssm.common.CompanyPage;
import com.ttc.ssm.common.Page;
import com.ttc.ssm.po.City;
import com.ttc.ssm.po.Company;
import com.ttc.ssm.po.CompanyDetail;
import com.ttc.ssm.po.Industry;
import com.ttc.ssm.service.admin.CompanyService;
import com.ttc.ssm.service.admin.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Created by Jiarui on 2016/7/25.
 */
@Controller
@RequestMapping("/admin/company")
public class CompanyController {

    @Autowired
    CompanyService companyService;

    @RequestMapping("/index")
    public String index(Model model){
        return "admin/company/index";
    }

    @RequestMapping("/verify")
    public String verify(Model model){
        return "admin/company/verify";
    }

    @RequestMapping("/getCompanyList")
    public @ResponseBody Map getCompanyList(@RequestParam("p") Integer page,@RequestParam("company_name") String company_name,@RequestParam("status") Integer status) throws Exception {
        System.out.println(page);
        CompanyPage companyPage = new CompanyPage();
        companyPage.setPageSize(20);
        companyPage.setCurrentPage(page);
        companyPage.setStatus(status);
        if(!(company_name==null)){
            companyPage.setCompany_name(company_name);
        }
        companyPage.setTotalRows(companyService.countRows(companyPage));
        List<Company> companies = companyService.selectCompanyForPage(companyPage);
        Map map = new HashMap();
        map.put("data",companies);
        map.put("show",companyPage);
        System.out.println(companies);
        return map;
    }

    @RequestMapping("/deleteCompanies")
    @ResponseBody
    public int deleteCompanies(@RequestBody Map ids,HttpServletRequest request) throws Exception{
       try {
           companyService.deleteCompanies(ids,request);
           return 1;
       }catch (Exception e){
           System.out.println(e);
           return 0;
       }
    }

    @RequestMapping("/updateCompanyStatus")
    @ResponseBody
    public int updateCompanyStatus(@RequestParam("company_id") Integer company_id,@RequestParam("status") Integer status) throws Exception{
        try {
            companyService.updateCompanyStatus(company_id,status);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @RequestMapping("/detail")
    public String CompanyDetail(@RequestParam("company_id") Integer company_id,Model model) throws Exception {
        Company company = companyService.selectCompanyById(company_id);
        model.addAttribute("company",company);
        CompanyDetail companyDetail = companyService.selectCompanyDetail(company_id);
        model.addAttribute("company_detail",companyDetail);
        Industry main_industry = new Industry();
        Industry sec_industry = new Industry();
        if(companyDetail != null) {
            main_industry = companyService.select_industry_name(companyDetail.getMain_industry_id());
            sec_industry = companyService.select_industry_name(companyDetail.getSec_industry_id());
        }
        model.addAttribute("main_industry",main_industry);
        model.addAttribute("sec_industry",sec_industry);
        return "admin/company/detail";
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam("company_id") Integer company_id,Model model) throws Exception{
        Company company = companyService.selectCompanyById(company_id);
        model.addAttribute("company",company);
        CompanyDetail companyDetail = companyService.selectCompanyDetail(company_id);
        model.addAttribute("company_detail",companyDetail);
        Industry main_industry = new Industry();
        Industry sec_industry = new Industry();
        if(companyDetail != null) {
            main_industry = companyService.select_industry_name(companyDetail.getMain_industry_id());
            sec_industry = companyService.select_industry_name(companyDetail.getSec_industry_id());
        }
        model.addAttribute("main_industry",main_industry);
        model.addAttribute("sec_industry",sec_industry);
        model.addAttribute("industry_list",companyService.getIndustry());
        model.addAttribute("zone_list",companyService.getZone());
        model.addAttribute("city_list",companyService.getCityByZoneId(company.getZone_id()));
        model.addAttribute("property_list",companyService.getCompanyProperty());
        model.addAttribute("size_list",companyService.getCompanySize());
        return "admin/company/edit";
    }

    @RequestMapping("/save_company")
    @ResponseBody
    public int save_company(@RequestBody Company company){
        try{
            if(company.getCity_id() == 0 || company.getContact()==null || company.getContact_sex()==null || company.getZone_num()==null || company.getPhone()==null || company.getTelephone()==null||company.getFullname()==null)
                return -1;
            Date d = new Date();
            company.setDate_modified(d);
            companyService.updateCompany(company);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @RequestMapping("/save_company_detail")
    @ResponseBody
    public Object save_company_detail(HttpServletRequest request,CompanyDetail companyDetail,@RequestParam(value = "input22") MultipartFile temp_pic) throws IOException {
        try{
            if(companyDetail.getMain_industry_id() == 0 ||companyDetail.getSec_industry_id() == 0 ||companyDetail.getCompany_property_id() == 0 ||companyDetail.getCompany_size_id() == 0 || companyDetail.getIndroduce()==null || companyDetail.getRecruit_contact()==null || companyDetail.getZone_num()==null || companyDetail.getPhone()==null || companyDetail.getTelephone()==null)
                return -1;
            String originalFilename = temp_pic.getOriginalFilename();
            CompanyDetail companyDetail1 = companyService.selectCompanyDetail(companyDetail.getCompany_id());
            if(temp_pic !=null && originalFilename !=null && originalFilename.length()>0) {
                //存储图片的物理路径
                String pic_path=request.getSession().getServletContext().getRealPath("upload/company/");
                //新的图片名称
                String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                System.out.println(pic_path + newFileName);
                //新图片
                File newFile = new File(pic_path + newFileName);

                //将内存的数据写入磁盘
                temp_pic.transferTo(newFile);
                companyDetail.setLicence_url(newFileName);
                if(companyDetail1.getLicence_url()!=null)
                    deleteLicence(companyDetail1,request);
            }
            Date d = new Date();
            companyDetail.setDate_modified(d);
            companyDetail.setStatus(-1);
            if(companyDetail1!=null)
                companyService.updateCompanyDetail(companyDetail);
            else
                companyService.insertCompanyDetail(companyDetail);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @RequestMapping("/getCityList")
    public @ResponseBody List<City> getCityList(Integer zone_id) throws Exception {
        return companyService.getCityByZoneId(zone_id);
    }

    @RequestMapping("/getVerifyList")
    public @ResponseBody Map getVerifyList(@RequestParam("p") Integer page,@RequestParam("company_name") String company_name,@RequestParam("status") Integer status) throws Exception {
        CompanyPage companyPage = new CompanyPage();
        companyPage.setPageSize(20);
        companyPage.setCurrentPage(page);
        companyPage.setStatus(status);
        if(!(company_name==null)){
            companyPage.setCompany_name(company_name);
        }
        companyPage.setTotalRows(companyService.countRowsVerify(companyPage));
        List<Company> companies = companyService.selectVerifyForPage(companyPage);
        Map map = new HashMap();
        map.put("data",companies);
        map.put("show",companyPage);
        System.out.println(companies);
        return map;
    }

    @RequestMapping("/verifyCompanyDetail")
    @ResponseBody
    public int verifyCompanyDetail(@RequestParam("company_id") Integer company_id,@RequestParam("status") Integer status){
        try{
            companyService.verifyCompanyDetail(status,company_id);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    public static void deleteLicence(CompanyDetail companyDetail, HttpServletRequest request) {

        if (!(companyDetail.getLicence_url() ==null)){
            String pic_path=request.getSession().getServletContext().getRealPath("upload/company/");
            String old_file_name = pic_path+companyDetail.getLicence_url();
            File old_File = new File(old_file_name);
            if(old_File.exists()){
                old_File.delete();
            }
        }
    }

}
